package cn.example.viewflipper;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ViewFlipper;

import java.util.zip.Inflater;

public class MainActivity extends AppCompatActivity {

    private ViewFlipper viewFlipper;
    private float startx;
    //image资源
    private int [] img_rs=new int[]{R.drawable.a,R.drawable.b,R.drawable.c,R.drawable.d} ;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        viewFlipper=findViewById(R.id.my_viewFliperr);
        for (int i = 0; i < img_rs.length; i++) {
            viewFlipper.addView(getImgview(img_rs[i]));
        }
        //viewFlipper.setFlipInterval(200);//间隔
        //viewFlipper.setInAnimation(); 设置动画文件
        //viewFlipper.setOutAnimation();
        //viewFlipper.startFlipping();//开始
    }

    /**
     * 监听手势滑动
     * @param event
     * @return
     */
    @Override
    public boolean onTouchEvent(MotionEvent event) {

        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                startx=event.getX();
                break;
            case MotionEvent.ACTION_MOVE:
                if((event.getX()-startx)>100){
                    viewFlipper.showPrevious();
                }
                if ((startx-event.getX())>100){
                    viewFlipper.showNext();
                }
                break;
            case MotionEvent.ACTION_UP:
                break;
        }

        return super.onTouchEvent(event);
    }

    private ImageView getImgview(int img_rs) {
        ImageView view = new ImageView(this);
        view.setBackgroundResource(img_rs);
        return view;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MainActivity.this.getMenuInflater().inflate(R.menu.menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()){
            case  R.id.it1:
                Intent intent = new Intent(MainActivity.this, Main2Activity.class);
                startActivity(intent);
                break;

        }
        return true;
    }
}
